#!/bin/bash

# 提示用户输入车辆数量
read -p "Enter the number of simulated vehicles (e.g., 17): " VEHICLE_NUM

# 验证输入是否为有效数字
if ! [[ "$VEHICLE_NUM" =~ ^[0-9]+$ ]]; then
    echo "Invalid input! Please enter a valid number."
    exit 1
fi

# 获取项目目录
PROJECT_DIR=$(cd "$(dirname "$0")" && pwd)
echo "当前脚本所在目录是: $PROJECT_DIR"

# ROS 环境设置
ROS_SETUP="/opt/ros/foxy/setup.bash"

# 输出文件
OUTPUT_FILE="biandui.sh"

# 提示用户选择是否启动一键式脚本
read -p "一键式启动脚本是否启动？(yes/no): " user_input

# 清空输出文件
> $OUTPUT_FILE

# 根据用户输入选择启动命令
case $user_input in
    [Yy]es|Y|y)  # 匹配 yes、Yes、Y、y
        # 生成包含启动命令的数组
        LAUNCH_COMMANDS=(
            "gnome-terminal -- bash -c \"source ${ROS_SETUP}; source ${PROJECT_DIR}/install/setup.bash; ros2 launch uav_swarms leader_node_launch.py vehicle_num:=${VEHICLE_NUM} start_node:=0 end_node:=${VEHICLE_NUM}; exec bash\""
            "gnome-terminal -- bash -c \"source ${ROS_SETUP}; source ${PROJECT_DIR}/install/setup.bash; ros2 launch uav_swarms multi_follow_node_launch.py vehicle_num:=${VEHICLE_NUM}; exec bash\""
            "gnome-terminal -- bash -c \"source ${ROS_SETUP}; source ${PROJECT_DIR}/install/setup.bash; ros2 launch uav_swarms communication_multirotor_node_multi_launch.py vehicle_num:=${VEHICLE_NUM}; exec bash\""
            "gnome-terminal -- bash -c \"source ${ROS_SETUP}; source ${PROJECT_DIR}/install/setup.bash; ros2 launch uav_swarms avoid_node_launch.py vehicle_num:=${VEHICLE_NUM}; exec bash\""
            "gnome-terminal -- bash -c \"source ${ROS_SETUP}; source ${PROJECT_DIR}/install/setup.bash; ros2 launch uav_swarms getlocalpos_node_launch.py vehicle_num:=${VEHICLE_NUM}; exec bash\""
        )
        ;;
    [Nn]o|N|n)   # 匹配 no、No、N、n
        # 如果选择了不启动一键式脚本，只启动必要节点
        LAUNCH_COMMANDS=(
            "gnome-terminal -- bash -c \"source ${ROS_SETUP}; source ${PROJECT_DIR}/install/setup.bash; ros2 launch uav_swarms leader_node_launch.py vehicle_num:=${VEHICLE_NUM}; exec bash\""
            "gnome-terminal -- bash -c \"source ${ROS_SETUP}; source ${PROJECT_DIR}/install/setup.bash; ros2 launch uav_swarms multi_follow_node_launch.py vehicle_num:=${VEHICLE_NUM}; exec bash\""
        )
        ;;
    *)           # 匹配其他输入
        echo "输入无效，请输入 yes 或 no"
        exit 1
        ;;
esac

# 生成命令并写入输出文件
for COMMAND in "${LAUNCH_COMMANDS[@]}"; do
    echo $COMMAND >> $OUTPUT_FILE
done

# 使生成的脚本文件可执行
chmod +x $OUTPUT_FILE

echo "Commands have been generated and saved to $OUTPUT_FILE. You can run the generated script."
